//
//  GEDateTextField.m
//  OMS
//
//  Created by goldeneye on 14/10/29.
//  Copyright (c) 2014年 ShangHai Goldeneye Techonology Co.,Ltd. All rights reserved.
//

#import "GEDateTextField.h"
#import "IQUIView+IQKeyboardToolbar.h"
#import "IQUIView+Hierarchy.h"

@interface GEDateTextField ()

@property (strong, nonatomic) UIDatePicker *customPicker;
@property (strong, nonatomic) UIView *keyboardView;

@property (strong, nonatomic) NSArray *fontAry;
@property (nonatomic) UIDatePickerMode pickerModel;

@property (strong, nonatomic) id target;
@property (assign, nonatomic) SEL action;

@end

@implementation GEDateTextField

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self initWithTextField];
    }
    return self;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self initWithTextField];
    }
    return self;
}

- (void)initWithTextField
{
    self.pickerModel = UIDatePickerModeDate;
    
    self.keyboardView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SuperWidth, 162)];
    self.inputView = self.keyboardView;
    
    self.customPicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 0, SuperWidth, 162)];
    self.customPicker.datePickerMode = self.pickerModel;
    [self.keyboardView addSubview:self.customPicker];
    
    [self addPreviousNextDoneOnKeyboardWithTarget:self previousAction:@selector(previousAction:) nextAction:@selector(nextAction:) doneAction:@selector(doneAction:) titleText:self.placeholder];
}

- (void)setDatePickerMode:(UIDatePickerMode)pickerModel
{
    self.pickerModel = pickerModel;
    self.customPicker.datePickerMode = self.pickerModel;
}

- (void)setDefaultDate:(NSDate*)date
{
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    if (self.pickerModel == UIDatePickerModeDate) {
        [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    } else if (self.pickerModel == UIDatePickerModeDateAndTime) {
        [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
    }
    
    [self.customPicker setDate:date];
    NSString *dateAndTime =  [dateFormatter stringFromDate:date];
    self.text = dateAndTime;
}

- (void)addDoneTarget:(id)target action:(SEL)action
{
    self.target = target;
    self.action = action;
}

-(void)doneAction:(UIBarButtonItem*)barButton
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    if (self.pickerModel == UIDatePickerModeDate) {
        [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    } else if (self.pickerModel == UIDatePickerModeDateAndTime) {
        [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
    }
    
    NSString *dateAndTime =  [dateFormatter stringFromDate:self.customPicker.date];
    self.text = dateAndTime;
    
    //关闭键盘
    [self resignFirstResponder];
    
    if (self.target && self.action)
        [self.target performSelector:self.action withObject:nil];
}

-(void)cancelAction:(UIBarButtonItem*)barButton
{
    [self resignFirstResponder];
    
}

/*! previousAction. */
-(void)previousAction:(id)button
{
    //previousAction
    NSArray *responder =[self responderSiblings];
    NSInteger index = [responder indexOfObject:self];
    if (index-1>=0) {
        UITextField *textField = [responder objectAtIndex:index-1];
        [textField becomeFirstResponder];
        [self doneAction:nil];
    }
}

/*! nextAction. */
-(void)nextAction:(id)button
{
    //nextAction
    //[self becomeFirstResponder];
    NSArray *responder =[self responderSiblings];
    NSInteger index = [responder indexOfObject:self];
    if (index<responder.count) {
        UITextField *textField = [responder objectAtIndex:index+1];
        [textField becomeFirstResponder];
        [self doneAction:nil];
    }
}

@end
